package com.define.ssh.action.terminalparam;


import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.define.ssh.action.admin.BaseAdminAction;
import com.define.ssh.common.JsonUtil;
import com.define.ssh.model.TerminalparamType;
import com.define.ssh.service.TerminalparamTypeService;
import com.define.ssh.util.Pager.Order;
import com.sxit.ecwx.core.utils.Formatu;

/**
 *  
 * 
 */
@ParentPackage("admin")
public class Terminalparam1Action extends BaseAdminAction {
	
	private static final long serialVersionUID = 4L;
	/** 页面传递参数过来*/
	protected int pageSize;
	protected int pageNum;


	@Resource(name = "terminalparamTypeServiceImpl")
	private TerminalparamTypeService terminalparamTypeService;
	
	/**
	 * 列表页
	 * 
	 * @return
	 * @throws Exception
	 */
	public String list() {
		
		try {
			
	
		HashMap<String, Object> searchMap = new HashMap<String, Object>();
		pager.setOrder(Order.desc);
		pager.setOrderBy("updateTime");
		pager.setSearchMap(searchMap);
		pager =terminalparamTypeService.findPager(pager);
		} catch (Exception e) {
			e.printStackTrace();
		}	
		
		return "terminalparamType";
	}
	
	
	/**
	 * 保存
	 * 
	 * @return
	 * @throws Exception
	 */
	public String save() {
		
		
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");  
		response.setCharacterEncoding("UTF-8");
		String terminaltype = super.getRequest().getParameter("terminaltype");
		String paramid = super.getRequest().getParameter("paramid");
		String paramname = super.getRequest().getParameter("paramname");
		String description = super.getRequest().getParameter("description");
		 Date updatetime = terminalparamTypeService.getUpdateTime("terminalparam_type");
		try {
		
			/* DetachedCriteria dt = DetachedCriteria.forClass(TerminalparamType.class);	
			 dt.add(Restrictions.eq("paramid",Integer.valueOf(paramid)));
			 List<TerminalparamType> userlist=terminalparamTypeService.findByCriteria(dt);
			if(userlist!=null&&userlist.size()>0){
				
				response.getWriter().write("参数ID已存在请重新输入!");
				return null;
			}*/
			
			TerminalparamType mwl=new TerminalparamType();
			mwl.setTerminaltype(terminaltype);
			mwl.setParamid(Integer.valueOf(paramid));
			mwl.setParamname(paramname);
			mwl.setDescription(description);
			//mwl.setUpdateType(1);////更新标示 1新增  2修改  3删除
			mwl.setUpdateTime(new Date());
			terminalparamTypeService.save(mwl);
			Long modfiytime=0l;
			if(updatetime==null)
				 modfiytime = 0l;
			else
			  modfiytime = updatetime.getTime() / 1000;
			
			  System.out.println("updatetime=="+updatetime);
			  byte[] by = new byte[10];
		      System.arraycopy(Formatu.int2byte(12), 0, by,0, 4); /*操作用户 */
			  System.arraycopy(Formatu.int2byte(modfiytime.intValue()), 0, by,4, 4); /*操作时间  */
			  short opertype=2;
			  System.arraycopy(Formatu.short2byte(opertype), 0, by,8, 2); /*操作类型 */
			  terminalparamTypeService.sendMsg(by, 1002);
			response.getWriter().write("true");
		
		} catch (Exception e) {
			e.printStackTrace();
		}	
		
		return null;
	}
	
	/**
	 *详情
	 * @return
	 */
	public String TerminalparamTypeInfo() {
		
		try { 
			String id = super.getRequest().getParameter("id");
			 DetachedCriteria dt = DetachedCriteria.forClass(TerminalparamType.class);	
			 dt.add(Restrictions.eq("id",Integer.valueOf(id)));
			 List<TerminalparamType> userlist=terminalparamTypeService.findByCriteria(dt);
			 if(userlist!=null&&userlist.size()>0){
				 TerminalparamType mwl= userlist.get(0);
			     return ajax(JsonUtil.beanToJson(mwl));
			 }
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/****
	 * 修改
	 */
	public String update() {
		
		try {
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");  
		response.setCharacterEncoding("UTF-8");
		String id = super.getRequest().getParameter("id");
		String terminaltype = super.getRequest().getParameter("terminaltype");
		String paramid = super.getRequest().getParameter("paramid");
		String paramname = super.getRequest().getParameter("paramname");
		String description = super.getRequest().getParameter("description");
		 DetachedCriteria dt = DetachedCriteria.forClass(TerminalparamType.class);	
		 Date updatetime = terminalparamTypeService.getUpdateTime("terminalparam_type");
		 dt.add(Restrictions.eq("id",Integer.valueOf(id)));
		 List<TerminalparamType> userlist=terminalparamTypeService.findByCriteria(dt);
		 if(userlist!=null&&userlist.size()>0){
			 TerminalparamType mwl= userlist.get(0);
			 mwl.setTerminaltype(terminaltype);
			 mwl.setParamid(Integer.valueOf(paramid)); 
			
			 mwl.setParamname(paramname);
			 mwl.setDescription(description);
			 //mwl.setUpdateType(1);////更新标示 1新增  2修改  3删除
			 mwl.setUpdateTime(new Date());
			 terminalparamTypeService.update(mwl);
			  Long modfiytime = updatetime.getTime() / 1000;
			  byte[] by = new byte[10];
		      System.arraycopy(Formatu.int2byte(0), 0, by,0, 4); /*操作用户 */
			  System.arraycopy(Formatu.int2byte(modfiytime.intValue()), 0, by,4, 4); /*操作时间  */
			  short opertype=2;
			  System.arraycopy(Formatu.short2byte(opertype), 0, by,8, 2); /*操作类型 */
			  terminalparamTypeService.sendMsg(by, 1002);
			 
			 response.getWriter().write("true");
		 }
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/****
	 * 删除
	 */
	public String delete() {
		
		try {
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");  
		response.setCharacterEncoding("UTF-8");
		
		String id = super.getRequest().getParameter("id");
		 DetachedCriteria dt = DetachedCriteria.forClass(TerminalparamType.class);	
		 dt.add(Restrictions.eq("id",Integer.valueOf(id)));
		 List<TerminalparamType> userlist=terminalparamTypeService.findByCriteria(dt);
		 if(userlist!=null&&userlist.size()>0){
			 TerminalparamType mwl= userlist.get(0);
			 terminalparamTypeService.delete(mwl);
			 response.getWriter().write("true");
		 }
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}